<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\RobotRequest;
use App\CustHub\Request\SeatRequest;
use App\CustHub\Service\ChSeatPackageService;
use App\CustHub\Service\ChSeatService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mine\Annotation\Resubmit;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 席位
 */
#[Controller(prefix: 'ch/seat'),ApiAuth,VerifySignature]
class SeatController extends BaseController
{
    #[Inject]
    protected ChSeatService $service;

    #[Inject]
    protected ChSeatPackageService $packageService;

    /**
     * 席位列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): ResponseInterface
    {
        return $this->success($this->service->list($this->request->all()));
    }

    #[PostMapping('getSeatNum')]
    public function getSeatNum()
    {
        return $this->success($this->service->getSeatNum($this->request->all()));
    }

    /**
     * 席位套餐列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getPackageList')]
    public function getSeatPackageList(): ResponseInterface
    {
        return $this->success($this->packageService->getPackageList((int)$this->request->input('seat_type',0)));
    }

    #[PostMapping('buy'),Resubmit,ChPermission('ch:seat:buy'),ChOperationLog]
    public function buy(SeatRequest $request)
    {
        return $this->success(['order_no' => $this->service->buy($request->all())]);
    }

    #[PostMapping('renewal'),Resubmit,ChPermission('ch:seat:renewal'),ChOperationLog]
    public function renewal(SeatRequest $request)
    {
        return $this->success(['order_no' => $this->service->renewal($request->all())]);
    }

    /**
     * 订单支付
     * @param SeatRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("pay")]
    public function pay(SeatRequest $request): ResponseInterface
    {
        return $this->success($this->service->pay($request->all()));
    }

    /**
     * 订单支付状态
     * @param SeatRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("checkPayStatus")]
    public function checkPayStatus(SeatRequest $request): ResponseInterface
    {
        return $this->success(['status' => $this->service->checkPayStatus($request->all())]);
    }

    #[PostMapping('record')]
    public function record()
    {
        return $this->success($this->service->record($this->request->all()));
    }

    /**
     * 自动续费开关
     * @param SeatRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('autoSwitch'),ChOperationLog]
    public function autoSwitch(SeatRequest $request): ResponseInterface
    {
        return $this->service->changeStatus($request->input('id'),$request->input('is_auto_renewal'),'is_auto_renewal') ? $this->success() : $this->error();
    }

    /**
     * 解绑
     * @param SeatRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('unbind'),ChOperationLog]
    public function unbind(SeatRequest $request): ResponseInterface
    {
        return $this->service->unbind($request->all()) ? $this->success() : $this->error();
    }
}